package edu.lingnan.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.api.R;
import edu.lingnan.dto.CollegeReq;
import edu.lingnan.entity.College;
import edu.lingnan.entity.Student;
import edu.lingnan.entity.Teacher;
import edu.lingnan.service.CollegeService;
import edu.lingnan.service.SeatService;
import edu.lingnan.service.StudentService;
import edu.lingnan.service.TeacherService;
import edu.lingnan.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName CollegeController
 * @Description 对学院的增删改操作
 * @Author kang
 * @Date 2022/7/11 下午 6:35
 * @Version 1.0
 */
@RestController
@RequestMapping("/college")
@Api(tags = "学院控制器")
public class CollegeController {

    @Resource
    private CollegeService collegeService;

    @Resource
    private TeacherService teacherService;

    @Resource
    private StudentService studentService;

    @GetMapping("list")
    @ApiOperation("获得所有学院（下拉框）")
    public Result list(){
        LambdaQueryWrapper<College> collegeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collegeLambdaQueryWrapper.eq(College::getStatus, 1);
        List<College> list = collegeService.list(collegeLambdaQueryWrapper);
        return new Result(true, list, "操作成功");
    }

    @PostMapping("modify")
    @ApiOperation("管理员修改学院名称")
    public Result modify(@RequestBody @ApiParam("传学院id和要修改的名称过来") CollegeReq collegeReq){
        if (ObjectUtils.isEmpty(collegeReq)){
            return new Result(false, null, "参数不完整");
        }
        //找到该学院原来的名字
        College oldCollege = collegeService.getById(collegeReq.getId());
        //更新学院名字
        College college = new College()
                .setName(collegeReq.getName());
        LambdaQueryWrapper<College> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(College::getId, collegeReq.getId());
        boolean update = collegeService.update(college, wrapper);
        if (update){
            //如果更新成功，则学生和老师的学院也要对应更新
            LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            studentLambdaQueryWrapper.eq(Student::getSCollege, oldCollege.getName());
            Student student = new Student();
            student.setSCollege(collegeReq.getName());
            studentService.update(student, studentLambdaQueryWrapper);
            //更新教师学院
            LambdaQueryWrapper<Teacher> teacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
            teacherLambdaQueryWrapper.eq(Teacher::getTCollege, oldCollege.getName());
            Teacher teacher = new Teacher();
            teacher.setTCollege(collegeReq.getName());
            teacherService.update(teacher, teacherLambdaQueryWrapper);
            return new Result(true, null, "修改成功");
        }else {
            return new Result(false, null, "修改失败");
        }
    }

    @PostMapping("/add/{name}")
    @ApiOperation("管理员新增一个学院")
    public Result add(@PathVariable @ApiParam("新增的学院的名称") String name){
        if (ObjectUtils.isEmpty(name)){
            return new Result(false, null, "参数错误");
        }
        College college = new College().setName(name);
        boolean save = collegeService.save(college);
        if (save){
            return new Result(true, null, "新增成功");
        }else {
            return new Result(false, null, "新增失败");
        }
    }

    @DeleteMapping("delete/{id}")
    @ApiOperation("管理员移除一个学院(逻辑删除)")
    public Result delete(@PathVariable @ApiParam("学院ID") Integer id){
        if (ObjectUtils.isNull(id)){
            return new Result(false, null, "参数错误");
        }
        LambdaQueryWrapper<College> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(College::getId, id);
        College college = new College().setStatus(0);
        boolean update = collegeService.update(college, wrapper);
        if (update){
            return new Result(true, null, "移除成功");
        }else {
            return new Result(true, null, "移除失败");
        }
    }
}
